package com.bridge.applets.config.redisson;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.Objects;
@Slf4j
@EnableConfigurationProperties(RedissonProperties.class)
@ConditionalOnClass(Config.class)
@Configuration
public class RedissonConfig {

    /**
     * 注册 single模式的 redisson 客户端
     *
     * @param redissonProperties redission阿波罗配置
     * @return redisson 客户端
     */
    @Primary
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "spring.redisson.mode", havingValue = "single")
    RedissonClient redissonClient(RedissonProperties redissonProperties) {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(redissonProperties.getNodeAddresses())
                .setIdleConnectionTimeout(redissonProperties.getIdleConnectionTimeout())
                .setConnectTimeout(redissonProperties.getConnectTimeout())
                .setRetryAttempts(redissonProperties.getRetryAttempts())
                .setRetryInterval(redissonProperties.getRetryInterval())
                //.setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
                .setTimeout(redissonProperties.getTimeOut())
                .setPingConnectionInterval(redissonProperties.getPingConnectionInterval());
        if (StringUtils.isNotEmpty(redissonProperties.getPassword())) {
            log.info("redissonClient  有密码 设置密码");
            serverConfig.setPassword(redissonProperties.getPassword());
        }
        log.info("redissonClient  配置");
        return Redisson.create(config);
    }
}
